package com.hlhj.funlike_business.activity.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.mymvp.mvp.BaseFgm;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.OrderDetailAty;
import com.hlhj.funlike_business.activity.adapter.AllOrderAdp;
import com.hlhj.funlike_business.bean.OrderListBean;
import com.hlhj.funlike_business.presenter.AllOrderFragmentPresenter;
import com.hlhj.funlike_business.views.AllOrderFragmentView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.widget.SpringView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class AllOrderFragment extends BaseFgm<AllOrderFragmentView, AllOrderFragmentPresenter> implements AllOrderFragmentView {
    private RecyclerView recycle_list;
    private AllOrderAdp allOrderAdp;
    private SpringView spring;
    private int page;
    private List<OrderListBean.DataBeanX.DataBean> dataBeanList;

    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_allorder;
    }

    @NotNull
    @Override
    public AllOrderFragmentView bindView() {
        return this;
    }

    @NotNull
    @Override
    public AllOrderFragmentPresenter createPresenter() {
        return new AllOrderFragmentPresenter();
    }

    @Override
    public void initView() {
        recycle_list = rootView.findViewById(R.id.recycle_list);
        spring = rootView.findViewById(R.id.spring);
    }

    @Override
    public void logicStart() {
        spring.setHeader(new DefaultFooter(getContext()));
        spring.setFooter(new DefaultFooter(getContext()));

        dataBeanList = new ArrayList<>();
        allOrderAdp = new AllOrderAdp(dataBeanList);
        recycle_list.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recycle_list.setAdapter(allOrderAdp);

        getPresenter().LoadOrderList(getContext(),0,page,10);
        allOrderAdp.setListener(new AllOrderAdp.getResult() {
            @Override
            public void showResult(int order_id, int status) {
                getPresenter().LoadResult(getContext(),order_id,status);
            }
        });
        allOrderAdp.setClick(new AllOrderAdp.getChageBack() {
            @Override
            public void showChargeBack(int order_id) {
                getPresenter().LoadChargeBack(getContext(),order_id);
            }
        });

    }

    @Override
    public void setListener() {
        spring.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getPresenter().LoadOrderList(getContext(),0,page,10);
                spring.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                page++;
                getPresenter().LoadOrderList(getContext(),0,page,10);
                spring.onFinishFreshAndLoad();
            }
        });
    }


    @Override
    public void showOrderList(List<OrderListBean.DataBeanX.DataBean> dataBeans) {
        if (page == 1) {
            dataBeanList.clear();
            dataBeanList.addAll(dataBeans);
        } else {
            dataBeanList.addAll(dataBeans);
        }
        allOrderAdp.notifyDataSetChanged();
    }

    @Override
    public void showResult(String msg) {
        Toast.makeText(getContext(),msg.toString(),Toast.LENGTH_SHORT).show();
        getPresenter().LoadOrderList(getContext(),0,1,10);
    }
}
